/******************************************/
/******************************************/
/****  JS Document                     ****/
/****  by Andrew Mahon                 ****/
/****  amahon@gmail.com                ****/
/******************************************/
/******************************************/

(function(tc) {

  tc.fn.y5infinteScroll = function(options){
    var $y5is = this, _o = {}
    
    _o = tc.jQ.extend({
      
    },options)
    
    function _initialize(){
      tc.util.log('y5infinteScroll._initialize')
      _setup_events()
      return $y5is
    }
    
    function _setup_events(){
      tc.util.log('y5infinteScroll._setup_events')
      tc.jQ(document).bind('scroll',_documentScrollHandler)
      tc.events.bind('dataLoad',_dataLoadHandler)
    }
    
    function _documentScrollHandler(e){
      //tc.util.log('y5infinteScroll._documentScrollHandler')
      if((tc.jQ(document).scrollTop()+tc.jQ(window).height()) > (tc.jQ(document).height() - 200)){
        tc.jQ(document).unbind('scroll',_documentScrollHandler)
        tc.events.trigger('fetchMoreFrames',{n_frames:2})
      }
    }
    
    function _dataLoadHandler(e,d){
      tc.util.log('y5infinteScroll._dataLoadHandler')
      tc.jQ(document).unbind('scroll',_documentScrollHandler).bind('scroll',_documentScrollHandler)
    }
    
    return _initialize()
  }
})(tc);